/*
 ============================================================================
 Name		: SymbianBSON.cpp
 Author	  : 
 Copyright   : 
 Description : Exe source file
 ============================================================================
 */

//  Include Files  

#include "SymbianBSON.h"
#include <e32base.h>
#include <e32std.h>
#include <e32cons.h>			// Console
#include <s32mem.h>

#include <f32file.h>
#include <s32file.h>


#include "BSON.h"
//  Constants

_LIT(KTextConsoleTitle, "Console");
_LIT(KTextFailed, " failed, leave code = %d");
_LIT(KTextPressAnyKey, " [press any key]\n");

//  Global Variables

LOCAL_D CConsoleBase* console; // write all messages to this


void TestEmptyDocumentL()
{
  CBufFlat* buffer = CBufFlat::NewL(1024);
  CleanupStack::PushL(buffer);
  RBufWriteStream out(*buffer);
  CleanupClosePushL(out);

  BSON::CDocument* document = BSON::CDocument::NewLC();
  document->SerializeL(out);

  TBuf8<1024> buf;
  buffer->Read(0, buf, buffer->Size());
  TInt length = buf.Length();
  ASSERT(length == 5);
  _LIT8(KTest, "\x05\x0\x0\x0\x0");
  ASSERT(buf == KTest());

  CleanupStack::PopAndDestroy(3, buffer);//document, out, buffer

  document = BSON::CDocument::NewLC();
  document->ParseL(buf);
  CleanupStack::PopAndDestroy(document);
}

void TestDocumentL()
{
  BSON::CDocument* document = BSON::CDocument::NewLC();
  BSON::CArray* array = BSON::CArray::NewLC();
  array->AppendL(_L("awesome"));
  array->AppendL(5.05);
  array->AppendL(1986);
  document->AppendL(_L("BSON"), array);
  CleanupStack::Pop(array);

  RFs fs;
  User::LeaveIfError(fs.Connect());
  CleanupClosePushL(fs);

  RFileWriteStream fileOut;
  User::LeaveIfError(fileOut.Replace(fs, _L("c:\\data\\others\\bson.data"), EFileWrite));
  CleanupClosePushL(fileOut);
  document->SerializeL(fileOut);
  CleanupStack::PopAndDestroy(&fileOut);

  RFileReadStream fileIn;
  User::LeaveIfError(fileIn.Open(fs, _L("c:\\data\\others\\bson.data"), EFileRead));
  CleanupClosePushL(fileIn);
  document->ParseL(fileIn);
  CleanupStack::PopAndDestroy(2, &fs);

  CleanupStack::PopAndDestroy(document);

//  document = BSON::CDocument::NewLC();
//  document->ParseL(buf);
//  CleanupStack::PopAndDestroy(document);
}

void TestL()
{
  TestEmptyDocumentL();
  TestDocumentL();
}

//  Local Functions

LOCAL_C void MainL()
{
  //
  // add your program code here, example code below
  //
  TestL();
}

LOCAL_C void DoStartL()
{
  // Create active scheduler (to run active objects)
  CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
  CleanupStack::PushL(scheduler);
  CActiveScheduler::Install(scheduler);

  MainL();

  // Delete active scheduler
  CleanupStack::PopAndDestroy(scheduler);
}

//  Global Functions

GLDEF_C TInt E32Main()
{
  // Create cleanup stack
  __UHEAP_MARK;
  CTrapCleanup* cleanup = CTrapCleanup::New();

  // Create output console
  TRAPD(createError, console = Console::NewL(KTextConsoleTitle, TSize(KConsFullScreen,
    KConsFullScreen)));
  if( createError )
  {
    delete cleanup;
    return createError;
  }

  // Run application code inside TRAP harness, wait keypress when terminated
  TRAPD(mainError, DoStartL());
  if( mainError )
    console->Printf(KTextFailed(), mainError);
  console->Printf(KTextPressAnyKey());
  console->Getch();

  delete console;
  delete cleanup;
  __UHEAP_MARKEND;
  return KErrNone;
}

